package com.lipaluma.example.clients.service;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lipaluma.example.clients.dao.IClientDAO;
import com.lipaluma.example.clients.model.Account;
import com.lipaluma.example.clients.model.Client;

@Service
@Transactional
public class ClientService {

	@Autowired
	private IClientDAO clientDAO;
	
	public Client findClientById(Long id) {
		return clientDAO.findOne(id);
	}
	
	public void addNewAccount(Client client, String accountLibelle) {
		Account account = new Account();
		account.setLibelle(accountLibelle);
		List<Account> accounts = client.getAccounts();
		account.setClient(client);
		if (accounts == null) {
			client.setAccounts(Arrays.asList(account));
		} else
			accounts.add(account);
		clientDAO.save(client);
	}

	public void createClient(String libelle) {
		Client client = new Client();
		client.setLibelle(libelle);
		clientDAO.save(client);
	}
	
}
